//
//  SignViewController.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/6.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "SignViewController.h"

#import "SignHeaderView.h"

#import "RecommendCell.h"

@interface SignViewController ()<UITableViewDelegate,UITableViewDataSource,SignHeaderViewDelegate>

@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)SignHeaderView * headerView;

@property(nonatomic,strong)NSArray * focuseProducts;

@end

@implementation SignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexColor(@"#EEEEEE");
    self.focuseProducts = [NSArray array];
    [self.view addSubview:self.tableView];
    [[LXDataManager manager] getSignDaysWithPrama:@{@"user_id":User_id} complete:^(BOOL success, id res) {
        if(success){
            [self.headerView setGreenBean:[[LXUser shareUser].bean_nums integerValue] remainedDay:7-[res[@"data"][@"signtimes"] integerValue]];
        }
    }];
    
    
    [[LXDataManager manager] listFocusProductWithComplete:^(BOOL success, id res) {
        if(success){
            self.focuseProducts = res;
            [self.tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
    self.tableView.contentInset = (UIEdgeInsets){-20,0,0,0};
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

#pragma mark - tableView代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.focuseProducts.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecommendCell * cell= [tableView dequeueReusableCellWithIdentifier:@"RecommendCell"];
    if(!cell){
        cell = [[RecommendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RecommendCell"];
    }
    cell.backgroundImageView.transform = CGAffineTransformIdentity;
    cell.enableUnaderLine = NO;
    [cell.backgroundImageView addRadius:0];
    if(indexPath.row == 0){
        cell.backgroundImageView.image = [UIImage imageNamed:@"brokerage_bg02"];
        cell.backgroundImageView.transform = CGAffineTransformRotate(cell.backgroundImageView.transform, M_PI);
        cell.enableUnaderLine = YES;
    }
    else if(indexPath.row == 9){
        cell.backgroundImageView.image = [UIImage imageNamed:@"brokerage_bg02"];
    }
    else{
        cell.backgroundImageView.image = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth-2*globalMargin, cell.cellHeight)];
        cell.enableUnaderLine = YES;
    }
    
    
    cell.product = self.focuseProducts[indexPath.row];
    [self addCellHeight:cell.cellHeight indexPath:indexPath];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return globalMargin;
}
-(UITableView*)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = 0;
        _tableView.sectionHeaderHeight = 0;
        _tableView.backgroundColor = HexColor(@"#EEEEEE");
        _tableView.tableHeaderView = self.headerView;
    }
    return _tableView;
}


-(SignHeaderView * )headerView{
    if(_headerView == nil){
        _headerView = [[SignHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, Sign_height(235))];
        _headerView.delegate = self;
    }
    return _headerView;
}

#pragma mark - headerView delegate
-(void)SignHeaderView:(SignHeaderView *)headerView didClickBackButton:(UIButton *)backButton{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)setUpNavi{
    UINavigationBar *  navigationBar = self.navigationController.navigationBar;
    navigationBar.translucent = YES;
    navigationBar.hidden = YES;
}

-(void)backButtonClick:(UIButton*)aButton{
    
}

@end
